package distributedbank;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import distributedbank.requests.Request;
import distributedbank.server.BankBranch;
/**
 * The RequestThread is responsible for reading a request object from the
 * server's incoming socket, and then executing the request on the given
 * BankBranch.
 */
public class RequestThread extends Thread
{
    private Socket incomingSocket;

    private BankBranch bankBranch;

    private Wrapper wrapper;
    
    public RequestThread( BankBranch bankBranch, Socket incomingSocket,
            Wrapper wrapper ) {
        this.incomingSocket = incomingSocket;
        this.bankBranch = bankBranch;
        this.wrapper = wrapper;
    }
    
    public void run() {
        try {
            ObjectInputStream in = new ObjectInputStream( this.incomingSocket
                    .getInputStream() );
            try {
                Request request = (Request) in.readObject();
                request.runRequest( this.bankBranch, this.wrapper );
            } catch( ClassNotFoundException e ) {
                System.out.println( e.getMessage() );
            } finally {
                in.close();
                this.incomingSocket.close();
            }
        } catch( IOException e ) {
            System.out.println( e.getMessage() );
        }
    }
}